<?php

namespace App\Http\Requests\Order;

use App\Http\Requests\ApiRequest;
use App\Http\Services\Api\V1\App\Product\AfterSaleService;
use App\Models\Customers\Customer;
use App\Models\Order\Order;
use App\Models\Order\OrderProduct;
use App\Models\Products\Product;
use App\Models\ReturnProduct\ReturnAction;
use App\Models\ReturnProduct\ReturnProduct;
use App\Models\ReturnProduct\ReturnStatus;
use App\Services\Utils\ApiChecker;
use App\Services\Utils\Guard;
use Carbon\Carbon;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;

/**
 * @property string product_id
 * @property string order_id
 * @property string return_reason_id
 * @property string quantity
 * @property string opened
 * @property string|null comment
 * @property string|null telephone
 * @property string|null fullname
 * @property string product
 * @property string model
 * @property string return_action_id
 * @property string return_status_id
 * @property string date_ordered
 * @property string date_added
 * @property string date_modified
 * @property string return_type_id
 * @property string order_product_id
 */
class ReturnProductStoreRequest extends ApiRequest
{
    use ApiChecker;

    /**
     * @var OrderProduct
     */
    public $order_product;

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     * @throws \Exception
     */
    public function rules()
    {
        $this->order_product = (new OrderProduct)
            ->where('order_product_id', $this->order_product_id)
            ->with('order')
            ->first();
        $max_quantity = $this->order_product->quantity ?? self::ERROR_INT_DEFAULT;

        $return_reason_ids = (new AfterSaleService)->getReturnReason()->pluck('return_reason_id');
        $return_action_ids = (new AfterSaleService)->getReturnAction()->pluck('return_action_id');

        return [
            'order_product_id' => 'required|int|min:1',
            'quantity'         => 'required|int|min:1|max:'.$max_quantity,
            'opened'           => 'required|int|in:0,1',
            'return_reason_id' => ['required', 'int', Rule::in($return_reason_ids)],
            'return_type_id'   => ['required', 'int', Rule::in($return_action_ids)],
            'comment'          => 'nullable|string',
            'telephone'        => 'nullable|string',
            'fullname'         => 'nullable|string',
        ];
    }

    /**
     * @param Validator $validator
     * @throws \Exception
     */
    protected function afterValidator(Validator $validator)
    {
        $this->addTableField();
    }

    /**
     * @throws \Exception
     */
    private function addTableField()
    {
        /** @var Customer $customer */
        $customer = Guard::customer();

        $now = (string) now();
        $order_product_id = $this->order_product->order_product_id;

        $order = $this->order_product->order ?? null;

        (new AfterSaleService)->isCanReturnProduct($order, $customer, $order_product_id);

        $this->return_action_id = $this->return_type_id;
        $this->return_status_id = ReturnStatus::getReturnStatusDefault($customer->store_id);
        $this->product = $this->order_product->name ?? '';
        $this->model = $this->order_product->getAttribute('model') ?? '';
        $this->date_ordered = $order->date_added;
        $this->date_added = $now;
        $this->date_modified = $now;
    }
}
